package scheduler;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Hashtable;

public class ProcParser {
    
    private String instructions = "";
    private String file, procName;
    private ArrayList<String> actions;
    private Hashtable<String, Process> procList;
    
    public String scanDirectory(String route) {
        try {
            
        } catch(Exception e) {
          System.out.println(e.getMessage()); 
        }
        return null;
    }
    
    public File[] finder( String dirName) throws FileNotFoundException {
       
    	File dir = new File(dirName);
        
        //listFiles es recursiva por que lo que obtenemos todos los archivos
        //designados como "proc" con terminacino .txt
    	return dir.listFiles(new FilenameFilter() { 
    	         public boolean accept(File dir, String filename)
    	              { 
                          if(filename.startsWith("proc")) {
                              try {
                                parseFile(filename);  
                              }
                              catch(Exception e) {
                              }
                          }
                          return filename.endsWith(".txt"); 
                      }
    	} );

    }
    
    public void parseFile(String filename) throws FileNotFoundException {
        try {
            String line = "";
            int counter = 0;
            BufferedReader reader = new BufferedReader(new FileReader("C:\\" + filename));
            do {
                line = reader.readLine();
                if(counter == 0) {
                    procName = line;
                }
                else{
                    actions.add(line);
                }
                counter++;
            } while(line != null);
            procList.put(procName, new Process(procName, actions));
            actions.clear();
        }
        catch (Exception e){
        }
    }
    
    public Hashtable getProcList(){
        return procList;
    }
}
